const { popularActivityDetail } = require('../../../../api/home');
const { popularActivityCollect, popularActivityCancelCollect, checkCollect } = require('../../../api/collect');
const { filePreview } = require('../../../../api/index');
const { sliceString, replaceImg } = require('../../../../utils/util');

// pages/active/detail/index.js
Page({
  data: {
    id: '',
    isCollcet: false,
    detail: {},
    shareModel:{
      title:'自定义组件分享title',
      path:'pages/test/test',
      imageUrl:'../../images/my/study.png',
      fromButton:'one'
    }
  },
  //  分享
  onShare() {},
  // 收藏
  async onCollect() {
    const obj = {
      studentid: wx.getStorageSync('studentid'),
      title: this.data.detail.title,
      targetId: this.data.detail.id,
      realName: wx.getStorageSync('userInfo').realName
    }

    //  点击 检查是否收藏过
    const res = await fetchCheckCollect.call(this, obj);

    if(res) { //  说明收藏了
      await fetchPopularActivityCancelCollect.call(this, obj);
    } else { // 说明未收藏
      await fetchPopularActivityCollect.call(this, obj);
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    const { id } = options;
    this.data.id = id;
    await fetchPopularActivityDetail.call(this, id);
    await initCollect.call(this);
  },
  /*** 用户点击右上角分享 */
  onShareAppMessage() {
    // let shareInfo = res.target.dataset.shareinfo;
    // return {
    //   title: shareInfo.title,
    //   path: shareInfo.path,
    //   imageUrl: shareInfo.imageUrl
    // }
  }
})

async function initCollect() {
  const obj = {
    studentid: wx.getStorageSync('studentid'),
    title: this.data.detail.title,
    targetId: this.data.detail.id,
    realName: wx.getStorageSync('userInfo').realName
  }

  const res = await fetchCheckCollect.call(this, obj);
  console.log(res);
  res ? this.setData({ isCollcet: true }) : this.setData({ isCollcet: false });
}

async function fetchPopularActivityDetail( id ) {
  const { errors = [], popularActivity } = await popularActivityDetail(id);
  if(errors.length > 0) {
    return;
  }

  this.setData({ detail: setResult(popularActivity) })
}

function setResult( data = {} ) {
  const _data = JSON.parse(JSON.stringify(data));
  _data.activityPhoto = filePreview( '/file/preview/' + _data.activityPhoto );
  _data.subtitle = sliceString(_data.subtitle);
  _data.introduction = replaceImg( _data.introduction );
  return _data;
}


// 检查 是否被收藏
async function fetchCheckCollect(data) {
  const { errors = [], result, collect = false } = await checkCollect( data );
  if( errors.length > 0 ) {
    return;
  }
  console.log(result)
  return result === '1'; //  判断是否收藏
}

//  收藏
async function fetchPopularActivityCollect(data) {
  const { errors = [], collect = false } = await popularActivityCollect( data );
  if( errors.length > 0 ) {
    myToast('取消收藏失败', 'error');
    return; 
  }

  myToast('收藏成功', 'success');
  this.setData({ isCollcet: true });
}

//  取消收藏
async function fetchPopularActivityCancelCollect(data) {
  const { errors = [], result, collect = false } = await popularActivityCancelCollect( data );
  if( errors.length > 0 ) {
    myToast('取消收藏失败', 'error');
    return;
  }

  myToast('取消收藏成功', 'success');
  //  改变图标的颜色
  this.setData({ isCollcet: false });
}

//  弹窗封装
function myToast(title, icon, duration = 1000) {
  wx.showToast({ title, icon, duration });
}